/**
 * 热区坐标信息
 * @author  韦胜健
 * @date    2022/3/4 22:42
 */
export interface iCmsRangeItemData {
  top: number,
  left: number,
  width: number,
  height: number,
}

/**
 * 百分比格式化，保留两位小数
 * @author  韦胜健
 * @date    2022/3/4 17:08
 */
export function formatNumber(val: number) {
  return Number((val * 100).toFixed(2));
}

/**
 * 判断是否存在覆盖
 * @author  韦胜健
 * @date    2022/3/4 18:04
 */
function isConflict(source: iCmsRangeItemData, target: iCmsRangeItemData) {
  /*x轴不相交*/
  const xNotConflict = (((source.left + source.width) < target.left) || (source.left > target.left + target.width));
  /*y轴不想交*/
  const yNotConflict = (((source.top + source.height) < target.top) || (source.top > target.top + target.height));

  /*x轴存在相交并且y轴也存在相交则证明存在覆盖区域*/
  return !xNotConflict && !yNotConflict;
}

/**
 * 找到重叠的节点
 * @author  韦胜健
 * @date    2022/3/5 9:50
 */
export function hasConflicts(data: iCmsRangeItemData, restrictZone: iCmsRangeItemData[]): boolean {
  return restrictZone.some(i => isConflict(i, data));
}
